﻿using System;
using ContractManager.BLL.Business;
using ContractManager.Common.Class;
using ContractManager.Common.FormBase;
using System.Windows.Forms;
using ContractManager.Entity;
using DevExpress.XtraEditors;

namespace ContractManager.Lists.Contracts
{
    public partial class FrmTenderDocumentsDetail : XtraFormBase
    {
        #region "Private Variable"
        
        public bool SaveClose;
        public bool SaveNew;
        public bool OnlyView;
        public int MasterId;

        #endregion

        #region "Form Construction"
        public FrmTenderDocumentsDetail()
        {
            InitializeComponent();
        }
        #endregion

        #region "Private Method"

        private void BlankForm()
        {
            /*txtDocTypId.EditValue = null;
            txtTenDocNo.EditValue = null;
            txtTenDocDate.EditValue = null;
            txtTenDocContent.EditValue = null;
            txtInvID.EditValue = null;
            txtWorkTypeId.EditValue = null;
            txtTenDocProject.EditValue = null;
            txtTenDocAddress.EditValue = null;
            txtTenDocCity.EditValue = null;
            
            txtMbID.EditValue = null;
            txtEmpId.EditValue = null;
            txtTenDocStatus.EditValue = null;
            txtTenDocDescription.EditValue = null;

            txtCustomField1.EditValue = null;
            txtCustomField2.EditValue = null;
            txtCustomField3.EditValue = null;
            txtCustomField4.EditValue = null;
            txtCustomField5.EditValue = null;
            txtCustomField6.EditValue = null;
            txtCustomField7.EditValue = null;
            txtCustomField8.EditValue = null;
            txtCustomField9.EditValue = null;
            txtCustomField10.EditValue = null;*/

            this.ResetControls();
            txtTenDocNo.Properties.ReadOnly = MasterId > 0;
        }

        private void LoadLookupData()
        {
            txtMbID.Properties.DataSource = ClsMemberBranch.SelectAll();
            txtMbID.Properties.DisplayMember = "MbName";
            txtMbID.Properties.ValueMember = "MbID";

            txtInvID.Properties.DataSource = ClsInvestors.SelectAll();
            txtInvID.Properties.DisplayMember = "InvName";
            txtInvID.Properties.ValueMember = "InvID";

            txtWorkTypeId.Properties.DataSource = ClsWorkType.SelectAll();
            txtWorkTypeId.Properties.DisplayMember = "WorkName";
            txtWorkTypeId.Properties.ValueMember = "WorkId";

            txtDocTypId.Properties.DataSource = ClsDocumentTypes.SelectAll();
            txtDocTypId.Properties.DisplayMember = "DocTypName";
            txtDocTypId.Properties.ValueMember = "DocTypId";

            txtEmpId.Properties.DataSource = ClsEmployees.SelectAll();
            txtEmpId.Properties.DisplayMember = "FullName";
            txtEmpId.Properties.ValueMember = "ID";

            txtTenDocCity.Properties.DataSource = ClsCitys.SelectAll();
            txtTenDocCity.Properties.DisplayMember = "CityName";
            txtTenDocCity.Properties.ValueMember = "CityId";
        }

        private void GetDetail()
        {
            var detail = ClsTenderDocuments.SelectByPk(MasterId);
            if (detail != null)
            {
                txtDocTypId.EditValue = detail.DocTypId;

                txtTenDocNo.EditValue = String.Format("{0}", detail.TenDocNo);
                txtTenDocDate.EditValue = detail.TenDocDate;
                txtTenDocContent.EditValue = String.Format("{0}", detail.TenDocContent);
                txtTenDocProject.EditValue = String.Format("{0}", detail.TenDocProject);
                txtWorkTypeId.EditValue = detail.WorkTypeId;
                txtInvID.EditValue = detail.InvestorId;
                txtTenDocAddress.EditValue = String.Format("{0}", detail.TenDocAddress);
                txtTenDocCity.EditValue = String.Format("{0}", detail.TenDocCity);
               
                txtMbID.EditValue = detail.MbID;
                txtEmpId.EditValue = detail.EmpId;
                txtTenDocStatus.EditValue = String.Format("{0}", detail.TenDocStatus);
                txtTenDocDescription.EditValue = String.Format("{0}", detail.TenDocDescription);

                txtTenDocNoTo.EditValue = String.Format("{0}", detail.TenDocNoTo);
                txtTenDocSymbolTo.EditValue = String.Format("{0}", detail.TenDocSymbolTo);
                txtTenDocDateTo.EditValue = detail.TenDocDateTo;

                txtCustomField1.EditValue = String.Format("{0}", detail.CustomField1);
                txtCustomField2.EditValue = String.Format("{0}", detail.CustomField2);
                txtCustomField3.EditValue = String.Format("{0}", detail.CustomField3);
                txtCustomField4.EditValue = String.Format("{0}", detail.CustomField4);
                txtCustomField5.EditValue = String.Format("{0}", detail.CustomField5);
                txtCustomField6.EditValue = String.Format("{0}", detail.CustomField6);
                txtCustomField7.EditValue = String.Format("{0}", detail.CustomField7);
                txtCustomField8.EditValue = String.Format("{0}", detail.CustomField8);
                txtCustomField9.EditValue = String.Format("{0}", detail.CustomField9);
                txtCustomField10.EditValue = String.Format("{0}", detail.CustomField10);  
            } 

        }

        private void DoSave()
        {
           
            #region "Check Condition"
            //if (!FormIsNotValidate)
            //{
            //    if (!dxValidationProvider.Validate())
            //    {
            //        XtraMessageBox.Show(
            //                    "Vui lòng nhập đầy đủ thông tin trước khi lưu",
            //                    "Thông báo",
            //                    MessageBoxButtons.OK,
            //                    MessageBoxIcon.Error);
            //        txtCode.SelectAll();
            //        txtCode.Focus();
            //        return;
            //    }
            //}
            
            #endregion

            var item = MasterId <= 0 ? new TenderDocuments() : ClsTenderDocuments.SelectByPk(MasterId);
            item.DocTypId = Convert.ToInt32(txtDocTypId.EditValue);
            item.TenDocNo = String.Format("{0}", txtTenDocNo.EditValue);
            item.TenDocDate = txtTenDocDate.DateTime;
            item.TenDocContent = String.Format("{0}", txtTenDocContent.EditValue);
            item.TenDocProject = String.Format("{0}", txtTenDocProject.EditValue);
            if (txtWorkTypeId.EditValue != null) item.WorkTypeId = Convert.ToInt32(txtWorkTypeId.EditValue);
            if (txtInvID.EditValue != null) item.InvestorId = Convert.ToInt32(txtInvID.EditValue);
           
            item.TenDocAddress = String.Format("{0}", txtTenDocAddress.EditValue);
            item.TenDocCity = String.Format("{0}", txtTenDocCity.EditValue);
            if (txtMbID.EditValue != null) item.MbID = Convert.ToInt32(txtMbID.EditValue);
            if (txtEmpId.EditValue != null) item.EmpId = Convert.ToInt32(txtEmpId.EditValue);
            
            item.TenDocStatus = String.Format("{0}", txtTenDocStatus.EditValue);
            item.TenDocDescription = String.Format("{0}", txtTenDocDescription.EditValue);

            item.TenDocNoTo = String.Format("{0}", txtTenDocNoTo.EditValue);
            item.TenDocSymbolTo = String.Format("{0}", txtTenDocSymbolTo.EditValue);

            if (txtTenDocDateTo.EditValue != null) item.TenDocDateTo = Convert.ToDateTime(txtTenDocDateTo.EditValue);

           

            item.CustomField1 = String.Format("{0}", txtCustomField1.EditValue);
            item.CustomField2 = String.Format("{0}", txtCustomField2.EditValue);
            item.CustomField3 = String.Format("{0}", txtCustomField3.EditValue);
            item.CustomField4 = String.Format("{0}", txtCustomField4.EditValue);
            item.CustomField5 = String.Format("{0}", txtCustomField5.EditValue);
            item.CustomField6 = String.Format("{0}", txtCustomField6.EditValue);
            item.CustomField7 = String.Format("{0}", txtCustomField7.EditValue);
            item.CustomField8 = String.Format("{0}", txtCustomField8.EditValue);
            item.CustomField9 = String.Format("{0}", txtCustomField9.EditValue);
            item.CustomField10 = String.Format("{0}", txtCustomField10.EditValue);

            item.FormId = FormNo;
            item.VoucherCate = VoucherCate;

            try
            {
                if (MasterId > 0)
                {
                    item.UpdateBy = ClsVariable.EmployeeId;
                    item.UpdateDate = DateTime.Now;
                    ClsTenderDocuments.Update(item); 
                }
                else
                {
                    item.CreateBy = ClsVariable.EmployeeId;
                    item.CreateDate = DateTime.Now;
                    ClsTenderDocuments.Insert(item);
                    MasterId = item.TenDocId;
                }

                
                if (SaveNew)
                {
                    MasterId = 0;
                    BlankForm();
                    txtTenDocNo.Focus();
                    
                }
                else if (SaveClose)
                {
                    Close();
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(
                    String.Format("Có lỗi xảy ra trong quá trình cập nhật dữ liệu. {0}", ex.Message),
                    "Thông báo",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
            }
        }

        private void DoDelete()
        {
            if (MasterId <= 0) return;
            
            try
            {
                if (
                        XtraMessageBox.Show(
                                        "Bạn có muốn xóa bản ghi này không?",
                                        "Thông báo",
                                        MessageBoxButtons.YesNo,
                                        MessageBoxIcon.Question) == DialogResult.No) return;
                ClsTenderDocuments.DeleteByPk(MasterId);
                Close();
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(
                       "Có lỗi xảy ra trong quá trình xóa dữ liệu. " + ex.Message,
                       "Thông báo",
                       MessageBoxButtons.OK,
                       MessageBoxIcon.Error);
            }
        }


        #endregion

        #region "Form Event"
        private void FrmTenderDocumentsDetailLoad(object sender, EventArgs e)
        {
            BlankForm();
            LoadLookupData();
            if (MasterId > 0) GetDetail();
            btnSaveNew.Enabled = !OnlyView;
            btnSave.Enabled = !OnlyView;
            btnSaveClose.Enabled = !OnlyView;
            btnDelete.Enabled = !OnlyView;
            btnDesign.Enabled = !OnlyView;
            if (!String.IsNullOrEmpty(FormXtraLayoutTemplate))
            {
                var fileLayout = ClsVariable.GetRestoreLayoutFullPath(FormXtraLayoutTemplate);
                layoutControl.RestoreLayoutFromXml(fileLayout);
            }
        }
        #endregion

        #region "Toolbar Event"
        private void TbMenuItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            switch (e.Item.Name)
            {
                case "btnSave":
                    DoSave();
                    break;
                case "btnSaveNew"://Lưu và thêm mới
                    SaveNew = true;
                    DoSave();
                    break;
                case "btnSaveClose"://Lưu và đóng
                    SaveClose = true;
                    DoSave();
                    break;
                case "btnDelete":
                    DoDelete();
                    break;
                case "btnDesign":
                    
                    break;
                case "btnRefresh"://Nạp lại danh mục
                    break;
                case "btnHistory"://History
                    break;
                case "btnClose":
                    Close();
                    break;
            }
        }
        #endregion

        

        
    }
}
